package two;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;



public class MonsterGame {

    static class Player {
        int hp;
        int attack;
        int defense;  // 防御力
        int intelligence;  // 智力
        int stamina;  // 体力
        int dodge;  // 躲避
        int level; // 等级
        int exp; // 经验值
        int skillPoints; // 技能点
        int gold; // 金币

        // ... 其他属性 ...
        int fireMagicLevel; // 火焰魔法等级
        int healMagicLevel; // 治疗魔法等级

        Equipment equippedWeapon; // 装备的武器
        Equipment equippedArmor; // 装备的护甲

        ArrayList<Item> inventory = new ArrayList<>(); // 添加一个背包


        Player(int hp, int attack, int defense, int intelligence, int stamina, int dodge) {
            this.hp = hp;
            this.attack = attack;
            this.defense = defense;
            this.intelligence = intelligence;
            this.stamina = stamina;
            this.dodge = dodge;
            this.level = 1;
            this.exp = 0;
            this.skillPoints = 0;
            this.gold = 100;
            // ... 初始化其他属性 ...
            this.fireMagicLevel = 0;
            this.healMagicLevel = 0;

            inventory.add(new Item("治疗药水", "healing", 20, 10)); // 添加治疗药水
            inventory.add(new Item("攻击药水", "attack", 10, 15)); // 添加攻击药水
//            inventory.add(new Equipment("木棒", "Weapon", 5, 0, 5)); // 添加一把基础武器
        }

        // ... 其他方法 ...
        // 添加一个向背包添加药水的方法
        public void addToInventory(Item Item) {
            inventory.add(Item);
            System.out.println("你获得了 " + Item.name + "！");
        }
        // 添加一个从背包移除药水的方法（当使用时）
        public Item removeFromInventory(Item Item) {
            inventory.remove(Item);
            return Item;
        }
        // 添加一个显示背包的方法
        public void displayInventory() {
            if (inventory.isEmpty()) {
                System.out.println("你的背包是空的！");
            } else {
                System.out.println("你的背包里包含：");
                for (Item Item : inventory) {
                    System.out.println("- " + Item.name);
                }
            }
        }

        // 装备物品
        public void equip(Equipment equipment) {
            if (equipment.type.equals("Weapon")) {
                this.equippedWeapon = equipment;
                this.attack += equipment.attackBonus;
                System.out.println("你装备了 " + equipment.name + "！");
            } else if (equipment.type.equals("Armor")) {
                this.equippedArmor = equipment;
                this.defense += equipment.defenseBonus;
                System.out.println("你装备了 " + equipment.name + "！");
            }
        }

        // 卸下物品
        public void unequip(Equipment equipment) {
            if (equipment.type.equals("Weapon") && this.equippedWeapon == equipment) {
                this.attack -= equipment.attackBonus;
                this.equippedWeapon = null;
                System.out.println("你卸下了 " + equipment.name + "！");
            } else if (equipment.type.equals("Armor") && this.equippedArmor == equipment) {
                this.defense -= equipment.defenseBonus;
                this.equippedArmor = null;
                System.out.println("你卸下了 " + equipment.name + "！");
            }
        }


        public void gainExp(int exp) {
            this.exp += exp;
            if (this.exp >= this.level * 30) { // 升级条件
                this.level++;
                this.skillPoints += 5; // 升级获得技能点
                this.defense += 5; // 升级提升防御力
                this.intelligence += 5; // 升级提升智力
                this.stamina += 10; // 升级提升体力
                this.dodge += 5; // 升级提升躲避

                System.out.println("你升到了 " + this.level + " 级！");
                // ... 升级提升属性值 ...
            }
        }

        // 学习技能
        public void learnSkill(int skillChoice) {
            if (skillChoice == 1) { // 学习火焰魔法
                if (this.skillPoints >= 5) {
                    this.fireMagicLevel++;
                    this.skillPoints -= 5;
                    System.out.println("你学会了火焰魔法！当前等级：" + this.fireMagicLevel);
                } else {
                    System.out.println("技能点不足，无法学习火焰魔法！");
                }
            } else if (skillChoice == 2) { // 学习治疗魔法
                if (this.skillPoints >= 5) {
                    this.healMagicLevel++;
                    this.skillPoints -= 5;
                    System.out.println("你学会了治疗魔法！当前等级：" + this.healMagicLevel);
                } else {
                    System.out.println("技能点不足，无法学习治疗魔法！");
                }
            } else {
                System.out.println("无效的技能选择！");
            }
        }
        // 使用火焰魔法
        public void useFireMagic(Monster monster) {
            if (this.fireMagicLevel > 0) {
                int damage = this.intelligence * this.fireMagicLevel; // 智力影响魔法伤害
                monster.hp -= damage;
                System.out.println("你使用了火焰魔法，造成了 " + damage + " 点伤害！");
            } else {
                System.out.println("你还没有学习火焰魔法！");
            }
        }
        // 使用治疗魔法
        public void useHealMagic() {
            if (this.healMagicLevel > 0) {
                this.hp += 50 * this.healMagicLevel;
                System.out.println("你使用了治疗魔法，恢复了 " + (50 * this.healMagicLevel) + " 点生命值！");
            } else {
                System.out.println("你还没有学习治疗魔法！");
            }
        }
        // 攻击方法
        public void attack(Monster monster) {

            Random random = new Random();

            if (random.nextInt(100) < monster.dodge) {
                System.out.println(monster.name + " 躲避了你的攻击！");
            } else {
                int damage = this.attack - monster.defense;
                if (damage > 0) {
                    monster.hp -= damage;
                    System.out.println("你攻击了 " + monster.name + "，造成了 " + damage + " 点伤害！");
                } else {
                    System.out.println("你的攻击被 " + monster.name + " 防御住了！");
                }
            }
        }
    }
    static class Equipment {
        String name; // 物品名称
        String type; // 物品类型（武器或护甲）
        int attackBonus; // 攻击力加成
        int defenseBonus; // 防御力加成
        int price; // 价格
        Equipment(String name, String type, int attackBonus, int defenseBonus, int price) {
            this.name = name;
            this.type = type;
            this.attackBonus = attackBonus;
            this.defenseBonus = defenseBonus;
            this.price = price;
        }
    }

    static class Item {
        String name;
        String type; // "治疗", "攻击", "防御", "体力", "闪避", "智力"
        int effect; // 效果值（治疗量、攻击加成等）
        int price;
        Item(String name, String type, int effect, int price) {
            this.name = name;
            this.type = type;
            this.effect = effect;
            this.price = price;
        }
    }
    static class Monster {
        int level=1;
        int hp;
        int attack;
        int defense;  // 防御力
        int intelligence;  // 智力
        int stamina;  // 体力
        int dodge;  // 躲避
        String name;
        int gold; // 金币

        Monster(int hp, int attack, int defense, int intelligence, int stamina, int dodge, String name, int gold) {
            this.hp = hp;
            this.attack = attack;
            this.defense = defense;
            this.intelligence = intelligence;
            this.stamina = stamina;
            this.dodge = dodge;
            this.name = name;
            this.gold = gold;
        }

        // 攻击方法
        public void attack(Player player) {

            Random random = new Random();

            if (random.nextInt(100) < player.dodge) {
                System.out.println("你躲避了 " + this.name + " 的攻击！");
            } else {
                int damage = this.attack - player.defense;
                if (damage > 0) {
                    player.hp -= damage;
                    System.out.println(this.name + " 攻击了你，造成了 " + damage + " 点伤害！");
                } else {
                    System.out.println(this.name + " 的攻击被你防御住了！");
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        // 创建玩家
        Player player = new Player(100, 35, 5, 30, 60, 30);  // 设置玩家属性

        while (player.hp > 0) { // 当玩家还有生命值时，继续战斗

            Monster monster = createMonster(random); // 创建怪物

            System.out.println("你遇到了一个 " + monster.name + "！");

            while (player.hp > 0 && monster.hp > 0) {
                // 玩家回合
                System.out.println("你的 HP：" + player.hp + "，" + monster.name + " 的 HP：" + monster.hp);
                System.out.println("请选择你的行动：1. 攻击 2. 使用技能 3. 使用道具 4. 查看状态 5. 商店 6. 学习技能");
                int choice = scanner.nextInt();

                if (choice == 1) {
                    // 玩家攻击
                    player.attack(monster);
                } else if (choice == 2) {
                    // 玩家使用技能
                    useSkill(player, monster, scanner);

                } else if (choice == 3) {
                    // 玩家使用道具
                    useItem(player, scanner);

                } else if (choice == 4) {
                    // 查看状态
                    displayPlayerStatus(player);

                } else if (choice == 5) {

                    // 进入商店
                    enterShop(player, scanner);

                } else if (choice == 6) {
                    // 学习技能
                    System.out.println("请选择要学习的技能：1. 火焰魔法 2. 治疗魔法");
                    int skillChoice = scanner.nextInt();
                    player.learnSkill(skillChoice);
                } else {
                    System.out.println("无效的选项！");
                }

                // 怪物回合
                if (monster.hp > 0) {
                    // 躲避判定
                    monster.attack(player);
                }
            }

            handleBattleResult(player, monster); // 处理战斗结果

        }
        System.out.println("游戏结束！");
        scanner.close();
    }

    // 创建怪物的方法
    private static Monster createMonster(Random random) {
        int monsterType = random.nextInt(3);
        switch (monsterType) {
            case 0: return new Monster(50, 10, 3, 5, 15, 5, "史莱姆", 10);
            case 1: return new Monster(70, 15, 7, 8, 20, 10, "哥布林", 20);
            case 2: return new Monster(100, 20, 10, 5, 25, 15, "狼人", 30);
            default: return null; // 应该添加异常处理
        }
    }

    // 处理战斗结果的方法
    private static void handleBattleResult(Player player, Monster monster) {

        Random random = new Random();


        if (monster.hp <= 0) {
            System.out.println("你战胜了 " + monster.name + "！ ");
            player.gainExp(monster.level * 10);
            player.gold += monster.gold;
            player.skillPoints += 5;

            if (random.nextDouble() < 0.5) { // 50% 概率掉落物品
                player.addToInventory(new Item("治疗药水", "healing", 20, 10));
            }
        } else {
            System.out.println("你被 " + monster.name + " 打败了！");
        }
    }

    // 进入商店的方法
    private static void enterShop(Player player, Scanner scanner) {
        System.out.println("欢迎来到商店！当前金币：" + player.gold);
        // 定义商店里的物品，但先不打印出来
        Item healingItem = new Item("治疗药水", "healing", 50, 10);
        Item attackItem = new Item("攻击药水", "attack", 15, 20);
        Item defenseItem = new Item("防御药水", "防御", 10, 15); // 防御药水
        Item staminaItem = new Item("体力药水", "体力", 20, 15); // 体力药水
        Item dodgeItem = new Item("闪避药水", "闪避", 5, 10);  // 闪避药水
        Item intelligenceItem = new Item("智力药水", "智力", 5, 10); // 智力药水
        Equipment woodSword = new Equipment("木剑", "Weapon", 10, 0, 10);
        Equipment leatherArmor = new Equipment("皮甲", "Armor", 0, 5, 10);
        Equipment ironSword = new Equipment("铁剑", "Weapon", 20, 0, 30);
        Equipment chainmail = new Equipment("锁子甲", "Armor", 0, 10, 50);
        Equipment dagger = new Equipment("匕首", "Weapon", 15, 0, 20);
        Equipment leatherBoots = new Equipment("皮靴", "Armor", 0, 3, 15);
        // 以清晰的格式展示商店里的物品
        System.out.println("1. " + woodSword.name + " (" + woodSword.price + " 金币)");
        System.out.println("2. " + leatherArmor.name + " (" + leatherArmor.price + " 金币)");
        System.out.println("3. " + ironSword.name + " (" + ironSword.price + " 金币)");
        System.out.println("4. " + chainmail.name + " (" + chainmail.price + " 金币)");
        System.out.println("5. " + dagger.name + " (" + dagger.price + " 金币)");
        System.out.println("6. " + leatherBoots.name + " (" + leatherBoots.price + " 金币)");
        System.out.println("7. " + healingItem.name + " (" + healingItem.price + " 金币)");
        System.out.println("8. " + attackItem.name + " (" + attackItem.price + " 金币)");
        System.out.println("9. " + defenseItem.name + " (" + defenseItem.price + " 金币)"); // 防御药水
        System.out.println("10. " + staminaItem.name + " (" + staminaItem.price + " 金币)"); // 体力药水
        System.out.println("11. " + dodgeItem.name + " (" + dodgeItem.price + " 金币)"); // 闪避药水
        System.out.println("12. " + intelligenceItem.name + " (" + intelligenceItem.price + " 金币)"); // 智力药水
        System.out.println("请选择你想要购买的物品：");
        int shopChoice = scanner.nextInt();


        if (shopChoice == 1) {
            // 木剑
            if (player.gold >= woodSword.price) {
                player.gold -= woodSword.price;
                player.equip(woodSword);
            } else {
                System.out.println("你的金币不足，无法购买" + woodSword.name + "！");
            }
        } else if (shopChoice == 2) {
            // 皮甲
            if (player.gold >= leatherArmor.price) {
                player.gold -= leatherArmor.price;
                player.equip(leatherArmor);
            } else {
                System.out.println("你的金币不足，无法购买" + leatherArmor.name + "！");
            }
        } else if (shopChoice == 3) {
            // 铁剑
            if (player.gold >= ironSword.price) {
                player.gold -= ironSword.price;
                player.equip(ironSword);
            } else {
                System.out.println("你的金币不足，无法购买" + ironSword.name + "！");
            }
        } else if (shopChoice == 4) {
            // 锁子甲
            if (player.gold >= chainmail.price) {
                player.gold -= chainmail.price;
                player.equip(chainmail);
            } else {
                System.out.println("你的金币不足，无法购买" + chainmail.name + "！");
            }
        } else if (shopChoice == 5) {
            // 匕首
            if (player.gold >= dagger.price) {
                player.gold -= dagger.price;
                player.equip(dagger);
            } else {
                System.out.println("你的金币不足，无法购买" + dagger.name + "！");
            }
        } else if (shopChoice == 6) {
            // 皮靴
            if (player.gold >= leatherBoots.price) {
                player.gold -= leatherBoots.price;
                player.equip(leatherBoots);
            } else {
                System.out.println("你的金币不足，无法购买" + leatherBoots.name + "！");
            }
        } else if (shopChoice == 7) {
            // 治疗药水
            if (player.gold >= healingItem.price) {
                player.gold -= healingItem.price;
                player.addToInventory(healingItem);
            } else {
                System.out.println("你的金币不足，无法购买" + healingItem.name + "！");
            }
        } else if (shopChoice == 8) {
            // 攻击药水
            if (player.gold >= attackItem.price) {
                player.gold -= attackItem.price;
                player.addToInventory(attackItem);
            } else {
                System.out.println("你的金币不足，无法购买" + attackItem.name + "！");
            }
        }
        else if (shopChoice == 9) {
            // 防御药水
            if (player.gold >= defenseItem.price) {
                player.gold -= defenseItem.price;
                player.addToInventory(defenseItem);
            } else {
                System.out.println("你的金币不足，无法购买" + defenseItem.name + "！");
            }
        } else if (shopChoice == 10) {
            // 体力药水
            if (player.gold >= staminaItem.price) {
                player.gold -= staminaItem.price;
                player.addToInventory(staminaItem);
            } else {
                System.out.println("你的金币不足，无法购买" + staminaItem.name + "！");
            }
        } else if (shopChoice == 11) {
            // 闪避药水
            if (player.gold >= dodgeItem.price) {
                player.gold -= dodgeItem.price;
                player.addToInventory(dodgeItem);
            } else {
                System.out.println("你的金币不足，无法购买" + dodgeItem.name + "！");
            }
        } else if (shopChoice == 12) {
            // 智力药水
            if (player.gold >= intelligenceItem.price) {
                player.gold -= intelligenceItem.price;
                player.addToInventory(intelligenceItem);
            } else {
                System.out.println("你的金币不足，无法购买" + intelligenceItem.name + "！");
            }
        }
        else {
            System.out.println("无效的商店选择！");
        }
    }

    // 使用道具的方法
    private static void useItem(Player player, Scanner scanner) {
        player.displayInventory(); // 显示玩家的背包
        System.out.println("请选择你的道具：");
        for (int i = 0; i < player.inventory.size(); i++) {
            System.out.println((i + 1) + ". " + player.inventory.get(i).name);
        }
        // ... (从库存显示药水) ...
        int itemChoice = scanner.nextInt();
        // 遍历背包，找到用户选择的物品
        Item Item = null;
        for (int i = 0; i < player.inventory.size(); i++) {
            if (i + 1 == itemChoice) { // 检查用户选择的物品是否与当前索引 + 1 匹配
                Item = player.inventory.get(i);
                break; // 找到物品后退出循环
            }
        }
        if (Item == null) { // 处理无效道具选择
            System.out.println("无效的道具选择！");
            return; // 退出方法
        }
        // 根据 Item.type 判断道具类型并执行相应操作
        if (Item.type.equals("healing")) { // 治疗药水
            player.hp += Item.effect;
            System.out.println("你使用了 " + Item.name + "，恢复了 " + Item.effect + " 点生命值！");
        } else if (Item.type.equals("attack")) { // 攻击药水
            player.attack += Item.effect;
            System.out.println("你使用了 " + Item.name + "，提升了 " + Item.effect + " 点攻击力！");
        } else if (Item.type.equals("防御")) { // 防御药水
            player.defense += Item.effect;
            System.out.println("你使用了 " + Item.name + "，提升了 " + Item.effect + " 点防御力！");
        } else if (Item.type.equals("体力")) { // 体力药水
            player.stamina += Item.effect;
            System.out.println("你使用了 " + Item.name + "，提升了 " + Item.effect + " 点体力！");
        } else if (Item.type.equals("闪避")) { // 闪避药水
            player.dodge += Item.effect;
            System.out.println("你使用了 " + Item.name + "，提升了 " + Item.effect + " 点闪避！");
        } else if (Item.type.equals("智力")) { // 智力药水
            player.intelligence += Item.effect;
            System.out.println("你使用了 " + Item.name + "，提升了 " + Item.effect + " 点智力！");
        } else {
            System.out.println("未知的道具类型！"); // 可以添加其他道具类型的判断
        }
        player.removeFromInventory(Item); // 使用后从背包移除
    }

    // 查看状态的方法
    private static void displayPlayerStatus(Player player) {
        System.out.println("你的状态：");
        System.out.println("HP：" + player.hp);
        System.out.println("攻击力：" + player.attack);
        System.out.println("防御力：" + player.defense);
        System.out.println("智力：" + player.intelligence);
        System.out.println("体力：" + player.stamina);
        System.out.println("躲避：" + player.dodge);
        System.out.println("等级：" + player.level);
        System.out.println("经验值：" + player.exp);
        System.out.println("技能点：" + player.skillPoints);
        System.out.println("金币：" + player.gold);
    }

    // 使用技能的方法
    private static void useSkill(Player player, Monster monster, Scanner scanner) {
        System.out.println("请选择你的技能：1. 治疗 2. 火焰魔法");
        int skillChoice = scanner.nextInt();
        if (skillChoice == 1) {
            player.useHealMagic();
        } else if (skillChoice == 2) {
            player.useFireMagic(monster);
        } else {
            System.out.println("无效的技能选择！");
        }
    }
}